我的Bilibili频道:香芋派Taro
我的个人博客:taropie0224.github.io(阅读体验更佳)
我的公众号:香芋派的烘焙坊
我的音频技术交流群:1136403177
我的个人微信:JazzyTaroPie
https://leetcode.cn/problems/product-of-array-except-self/
题解and思路
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| class Solution { public: vector<int> productExceptSelf(vector<int>& nums) { int length = nums.size();
vector<int> L(length), R(length, 0); vector<int> answer(length);
L[0] = 1; for (int i = 1; i < length; i++) { L[i] = nums[i - 1] * L[i - 1]; }
R[length - 1] = 1; for (int i = length - 2; i >= 0; i--) { R[i] = nums[i + 1] * R[i + 1]; }
for (int i = 0; i < length; i++) { answer[i] = L[i] * R[i]; }
return answer; } };
|